package com.ve.searcher.lucutil

import org.apache.lucene.analysis.Analyzer
//import org.apache.lucene.analysis.core.SimpleAnalyzer
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.index.IndexReader
import org.apache.lucene.index.IndexWriter
import org.apache.lucene.index.IndexWriterConfig
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.store.FSDirectory
import org.appsugar.archetypes.extension.getLogger

import java.nio.file.Paths


object LuceneInit {
    // Lucene variables
    var index: FSDirectory? = null
        private set
    var analyzer: Analyzer? = null
        private set
    var indexWriterConfig: IndexWriterConfig? = null
        private set
    var indexWriter: IndexWriter? = null
        private set
    val indexReader: IndexReader? = null
    val indexSearcher: IndexSearcher? = null


    init {
        try {
            index = FSDirectory.open(Paths.get(Constants.PATH + "lucene.index"))
            // todo analyzer for stemming and synonyms
            //            analyzer = MyAnalyzer.testAnalyzer();
            //            analyzer = MyAnalyzer.stemAnalyzer();
            //            analyzer = new WhitespaceAnalyzer();
//            analyzer = SimpleAnalyzer()
                        analyzer = StandardAnalyzer()
            //            analyzer = MyAnalyzer.wrapperAnalyzer();
            //            analyzer = new WhitespaceAnalyzer();

            indexWriterConfig = IndexWriterConfig(analyzer)
            indexWriter = IndexWriter(index!!, indexWriterConfig!!)

            indexWriter!!.commit()
        } catch (e: Exception) {
            getLogger<LuceneInit>().error(e.message,e)
        }

    }
}
